#include "CInputPlayer.h"
#include "GameSceneManager.h"
#include "GameSystemManager.h"
#include "GameObject.h"
#include "CPosition.h"
#include "COrientation.h"
#include "CVelocity.h"

/*static*/ 
comp_id_type CInputPlayer::mFamilyID = "CInputPlayer";
comp_id_type CInputPlayer::mComponentID = "CInputPlayer";

comp_id_type CInputPlayerTemplate::mFamilyID = "CInputPlayer";
comp_id_type CInputPlayerTemplate::mComponentID = "CInputPlayer";
/*static*/ 

SceneNode* CInputPlayer::cameraNode = NULL;
SceneNode* CInputPlayer::cameraYawNode = NULL;
SceneNode* CInputPlayer::cameraPitchNode = NULL;
SceneNode* CInputPlayer::cameraRollNode = NULL;

OgreText *CInputPlayer::debug = NULL;

GameComponent *CInputPlayerTemplate::makeComponent() {
	CInputPlayer *comp = new CInputPlayer(this);
	return comp;
}

CInputPlayer::CInputPlayer()
{
	
	debug = new OgreText("debug1");
}
CInputPlayer::CInputPlayer(CInputPlayerTemplate *templ)
{

}
CInputPlayer::~CInputPlayer()
{

}

void CInputPlayer::update() {
	//std::string a = getOwnerObject()->getID();

	if(!GameSystemManager::getSingleton()->getInput()->isRightButtonDown()) {
		GameSystemManager::getSingleton()->getGUI()->showCursor();
		return;
	}

	GameSystemManager::getSingleton()->getGUI()->hideCursor();

	CPosition *mCPosition = dynamic_cast<CPosition*>(getOwnerObject()->getComponent("CPosition"));
	COrientation *mCOrientation = dynamic_cast<COrientation*>(getOwnerObject()->getComponent("COrientation"));
	CVelocity *mCVelocity = dynamic_cast<CVelocity*>(getOwnerObject()->getComponent("CVelocity"));

	Ogre::Real pitchAngle;
	Ogre::Real pitchAngleSign;

	Vector3 mTranslateVector = Vector3::ZERO;

	Real time = GameSystemManager::getSingleton()->getTime(); // * 0.001f

	if (GameSystemManager::getSingleton()->getInput()->forward)
		mTranslateVector.z = -0.1 * time * mCVelocity->Velocity; //390.0f
	if (GameSystemManager::getSingleton()->getInput()->backward)
		mTranslateVector.z = 0.1 * time * mCVelocity->Velocity;
	if (GameSystemManager::getSingleton()->getInput()->right)
		mTranslateVector.x = 0.1 * time * mCVelocity->Velocity;
	if (GameSystemManager::getSingleton()->getInput()->left)
		mTranslateVector.x = -0.1 * time * mCVelocity->Velocity;
	if (GameSystemManager::getSingleton()->getInput()->up)
		mTranslateVector.y = -0.1 * time * mCVelocity->Velocity;
	if (GameSystemManager::getSingleton()->getInput()->down)
		mTranslateVector.y = 0.1 * time * mCVelocity->Velocity;
	
	cameraYawNode->yaw(GameSystemManager::getSingleton()->getInput()->Yaw);
	cameraPitchNode->pitch(GameSystemManager::getSingleton()->getInput()->Pitch);
	cameraNode->translate(cameraYawNode->getOrientation() *	cameraPitchNode->getOrientation() *	mTranslateVector,Ogre::SceneNode::TS_LOCAL);
		
	Vector3 camp; // = mCamera->getPosition();
	camp.x = cameraNode->getPosition().x;
	camp.y = cameraNode->getPosition().y;
	camp.z = cameraNode->getPosition().z;
	cameraNode->setPosition(camp);
		
	// Angle of rotation around the X-axis.
	pitchAngle = (2 * Ogre::Degree(Ogre::Math::ACos(cameraPitchNode->getOrientation().w)).valueDegrees());

	// Just to determine the sign of the angle we pick up above, the
	// value itself does not interest us.
	pitchAngleSign = cameraPitchNode->getOrientation().x;

	// Limit the pitch between -90 degress and +90 degrees, Quake3-style.
	if (pitchAngle > 90.0f)
	{
		if (pitchAngleSign > 0)
			// Set orientation to 90 degrees on X-axis.
			cameraPitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
																   Ogre::Math::Sqrt(0.5f), 0, 0));
		else if (pitchAngleSign < 0)
			// Sets orientation to -90 degrees on X-axis.
			cameraPitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
																   -Ogre::Math::Sqrt(0.5f), 0, 0));
	}

	//mTranslateVector = Ogre::Vector3::ZERO;
	//GameSceneManager::getSingleton()->getCameraNode()->setOrientation(cameraPitchNode->getOrientation() * camera

	//GameSystemManager::getSingleton()->getInput()->Yaw = 0;
	//GameSystemManager::getSingleton()->getInput()->Pitch = 0;
	
	//OgreQueueText::instance()->Addf("Camera: %i,%i,%i",camp.x,camp.y,camp.z);
	//OgreQueueText::instance()->clearItems();
	debug->showTextNormal("camera: " + StringConverter::toString(camp.x) + " " + StringConverter::toString(camp.y) + " " + StringConverter::toString(camp.z),10,10);
}

void CInputPlayer::setup() {
	
	Vector3 pos(600,300,280); // = mCamera->getPosition();
	
	// Create the camera's top node (which will only handle position).
	cameraNode = GameSceneManager::getSingleton()->getSceneManager()->getRootSceneNode()->createChildSceneNode();
	cameraNode->setPosition(pos);

	// Create the camera's yaw node as a child of camera's top node.
	cameraYawNode = cameraNode->createChildSceneNode();

	// Create the camera's pitch node as a child of camera's yaw node.
	cameraPitchNode = cameraYawNode->createChildSceneNode();

	// Create the camera's roll node as a child of camera's pitch node
	// and attach the camera to it.
	cameraRollNode = cameraPitchNode->createChildSceneNode();
	cameraRollNode->attachObject(GameSceneManager::getSingleton()->getCamera());
}

